package com.arkisv.log.mq.receiver;

import com.arkisv.common.model.ParamLog;
import com.arkisv.log.dao.ParamLogMapper;
import com.arkisv.log.util.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

/**
 * 参数日志接收器
 * Created by fb on 2017/1/23.
 */
@Component
public class ParamLogReceiver implements MessageListener {
    @Autowired
    private ParamLogMapper paramLogDao;
    @Override
    @JmsListener(destination = "param_log")
    public void onMessage(Message message) {
        ObjectMessage om = (ObjectMessage) message;
        try {
            ParamLog paramLog = (ParamLog) om.getObject();
            if(paramLog == null || paramLog.getEventId() == null) {
                return;
            }
            
            paramLog.setParams( Global.sub(paramLog.getParams(), Global.PARAM_PARAMS_LENGTH) );
            paramLog.setCookie( Global.sub(paramLog.getCookie(), Global.PARAM_COOKIE_LENGTH) );
            paramLogDao.insertSelective(paramLog);
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}
